trigger CheckOutletHasPlan on RoutingPlan__c (before insert,after delete) 
{
    //try
    //{
        //if(trigger.new.size() > 0 || trigger.old.size()>0)
        //{
            Set<Id> outletID=new Set<Id>();
            Set<Date> StartPlanAndVisitDateList=new Set<Date>();
            Set<Date> EndPlanAndVisitDateList=new Set<Date>();
            String filter='',outletFilter='',StarDateStr='',EndDateStr='';
            Integer year=0,month=0,day=0;
            
            if(trigger.isInsert)
            {
                for(RoutingPlan__c rp:trigger.new)
                {
                    if(!outletID.contains(rp.OutletName__c))
                    {
                        if(outletFilter!='')
                            outletFilter+=' , ';
                        
                        outletFilter+='\''+rp.OutletName__c+'\'';
                        
                        outletID.add(rp.OutletName__c);
                    }
                    
                    if(!StartPlanAndVisitDateList.contains(rp.Plan_Visit_Date__c.toStartOfMonth()))
                    {
                        if(rp.Plan_Visit_Date__c.Month()<10)
                        {
                            StarDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-0'+String.valueOf(rp.Plan_Visit_Date__c.Month())+'-01';
                            EndDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-0'+String.valueOf(rp.Plan_Visit_Date__c.Month()+1)+'-01';
                        }
                        else
                        {
                            StarDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-'+String.valueOf(rp.Plan_Visit_Date__c.Month())+'-01';
                            EndDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-'+String.valueOf(rp.Plan_Visit_Date__c.Month()+1)+'-01';
                        }   
                        
                        if(filter != '')
                            filter+=' or ';
                            
                        filter+=' (Plan_Visit_Date__c >='+StarDateStr+' and Plan_Visit_Date__c<'+EndDateStr+')';
                       
                        StartPlanAndVisitDateList.add(rp.Plan_Visit_Date__c.toStartOfMonth());
                    }
                }
            }
            else
            {
                for(RoutingPlan__c rp:trigger.Old)
                {
                    if(!outletID.contains(rp.OutletName__c))
                    {
                        if(outletFilter!='')
                            outletFilter+=' , ';
                        
                        outletFilter+='\''+rp.OutletName__c+'\'';
                        
                        outletID.add(rp.OutletName__c);
                    }
                    
                    if(!StartPlanAndVisitDateList.contains(rp.Plan_Visit_Date__c.toStartOfMonth()))
                    {
                        if(rp.Plan_Visit_Date__c.Month()<10)
                        {
                            StarDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-0'+String.valueOf(rp.Plan_Visit_Date__c.Month())+'-01';
                            EndDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-0'+String.valueOf(rp.Plan_Visit_Date__c.Month()+1)+'-01';
                        }
                        else
                        {
                            StarDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-'+String.valueOf(rp.Plan_Visit_Date__c.Month())+'-01';
                            if(rp.Plan_Visit_Date__c.Month()+1>12)
                                 EndDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year()+1)+'-01-01';
                            else
                                EndDateStr=String.valueOf(rp.Plan_Visit_Date__c.Year())+'-'+String.valueOf(rp.Plan_Visit_Date__c.Month()+1)+'-01';
                        }   
                        
                        if(filter != '')
                            filter+=' or ';
                            
                        filter+=' (Plan_Visit_Date__c >='+StarDateStr+' and Plan_Visit_Date__c<'+EndDateStr+')';
                       
                        StartPlanAndVisitDateList.add(rp.Plan_Visit_Date__c.toStartOfMonth());
                    }
                }
                
            }
            
            String sql='select OutletName__c,HasBeenPlanedInAMonth__c,Plan_Visit_Date__c '+
                               ' from RoutingPlan__c where ('+filter+') and OutletName__c in ('+outletFilter+') order by Plan_Visit_Date__c';
          system.debug(sql);
            List<RoutingPlan__c>  RoutingPlanList=DataBase.query(sql);
            //string aa='select OutletName__c,HasBeenPlanedInAMonth__c '+
            //                                      ' from RoutingPlan__c where '+filter+' and OutletName__c in ('+outletFilter+') order by Plan_Visit_Date__c';
            //System.assert(false,aa);                              
            Map<String,RoutingPlan__c> outletIdList=new Map<String,RoutingPlan__c>();
            if(RoutingPlanList!=NULL)
            {
                
                for(RoutingPlan__c routingplan:RoutingPlanList)
                {
                    outletIdList.put(String.valueOf(routingplan.Plan_Visit_Date__c.toStartOfMonth())+routingplan.OutletName__c,routingplan);
                }
            }
            if(trigger.isInsert)
            {
                for(RoutingPlan__c rp:trigger.new)
                {
                    if(outletIdList.containsKey(String.valueOf(rp.Plan_Visit_Date__c.toStartOfMonth())+rp.OutletName__c))
                    {
                        rp.HasBeenPlanedInAMonth__c=0;
                    }
                    else
                    {
                        rp.HasBeenPlanedInAMonth__c=1;
                        outletIdList.put(String.valueOf(rp.Plan_Visit_Date__c.toStartOfMonth())+rp.OutletName__c,rp);
                    }
                }
            }
            else
            {
                List<RoutingPlan__c> updateRoutingPlanList=new List<RoutingPlan__c>();
                for(RoutingPlan__c rp:trigger.Old)
                {
                    if(rp.HasBeenPlanedInAMonth__c==1)
                    {
                        if(outletIdList.containsKey(String.valueOf(rp.Plan_Visit_Date__c.toStartOfMonth())+rp.OutletName__c))
                        {
                            RoutingPlan__c updateRP=outletIdList.get(String.valueOf(rp.Plan_Visit_Date__c.toStartOfMonth())+rp.OutletName__c);
                            updateRP.HasBeenPlanedInAMonth__c=1;
                            updateRoutingPlanList.add(updateRP);
                        }
                    }
                }
                
                if(updateRoutingPlanList.size()>0)
                    update updateRoutingPlanList;
            }
        //}
    //}
    //catch(Exception ex)
    //{
    //  System.debug('*******************************************'+'设置路线计划出错，Trigger(CheckOutletHasPlan)');
    //}
}   
    /*if(trigger.new.size() > 0)
    {
        Datetime planDate = trigger.new[0].StartDateTime__c;
        Datetime dateStartOfMonth = planDate.date().toStartOfMonth();
        Datetime dateEndOfMonth = dateStartOfMonth.addMonths(1);
        // 查询当前计划所在月内所有的门店相关的拜访计划    
        string soql = 'Select OutletName__c, HasBeenPlanedInAMonth__c From RoutingPlan__c where StartDateTime__c >=' 
            + dateStartOfMonth.format('yyyy-MM-dd')+ 'T'+ dateStartOfMonth.format('hh:mm:ss')+ 'Z' 
            + ' and StartDateTime__c < ' + dateEndOfMonth.format('yyyy-MM-dd')+ 'T'+ dateEndOfMonth.format('hh:mm:ss')+ 'Z' 
            + ' and OutletName__c in (';
        for(RoutingPlan__c plan : trigger.new)
        {
            soql += '\'' + plan.OutletName__c + '\',';
        }
        soql = soql.substring(0, soql.length() - 1) + ') order by StartDateTime__c';
        List<RoutingPlan__c> existedPlans = Database.query(soql);
        
        // 记录已存在的每个计划的门店及其是否有计划
        Map<Id, Decimal> outletsAndFlag = new Map<Id, Decimal>();
        if(existedPlans != null && existedPlans.size() > 0)
        {
            for(RoutingPlan__c existedPlan : existedPlans)
            {
                if(outletsAndFlag.containsKey(existedPlan.OutletName__c))
                {
                    if(existedPlan.HasBeenPlanedInAMonth__c == 1)
                    {
                        outletsAndFlag.put(existedPlan.OutletName__c, existedPlan.HasBeenPlanedInAMonth__c);
                    }
                }
                else
                {
                    if(existedPlan.HasBeenPlanedInAMonth__c == null)
                    {
                        outletsAndFlag.put(existedPlan.OutletName__c, 0);
                    }
                    else
                    {
                        outletsAndFlag.put(existedPlan.OutletName__c, existedPlan.HasBeenPlanedInAMonth__c);
                    }
                }
            }
        }
        
        for(RoutingPlan__c newPlan : trigger.new)
        {
            System.debug(outletsAndFlag.get(newPlan.OutletName__c));
            // 查看本次新建的计划中是否有新门店
            if(outletsAndFlag.containsKey(newPlan.OutletName__c))
            {
                // 有当前门店，但没有置标志位，即标志位是0，则在新计划中置标志位为1
                if(outletsAndFlag.get(newPlan.OutletName__c) == null || outletsAndFlag.get(newPlan.OutletName__c) == 0)
                {
                    newPlan.HasBeenPlanedInAMonth__c = 1;
                }
                else
                {
                    newPlan.HasBeenPlanedInAMonth__c = 0;
                }
            }
            else // 没有当前门店，计划置标志位1
            {
                newPlan.HasBeenPlanedInAMonth__c = 1;
            }
        }
    }*/